//
//  SNMainTabBarController.m
//  SinaVboc
//
//  Created by V on 29/11/2016.
//  Copyright © 2016 仁忄. All rights reserved.
//

#import "SNMainTabBarController.h"
#import "SNMainNaviController.h"
#import "SNHomeTabBar.h"

@interface SNMainTabBarController ()

@end

@implementation SNMainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITabBar *myTabbar = [[SNHomeTabBar alloc] init];
    
    [self setValue:myTabbar forKey:@"TabBar"];
    
    [self addChildViewControllers];
}


- (void)addChildViewControllers {
    
    NSArray *array = @[@{@"class": @"SNHomeViewController", @"title": @"主页", @"image": @"home"},
                       @{@"class": @"SNMessageViewController", @"title": @"消息", @"image": @"message_center"},
                       @{@"class": @"SNDiscoverViewController", @"title": @"发现", @"image": @"discover"},
                       @{@"class": @"SNProfileViewController", @"title": @"我", @"image": @"profile"}];
    
    NSMutableArray *mArray = [NSMutableArray array];
    
    [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UIViewController *viewController = [self addChildControllerWithDict:array[idx]];
        
        [mArray addObject:viewController];
    }];
    
    self.viewControllers = mArray.copy;
}


- (UIViewController *)addChildControllerWithDict:(NSDictionary *)dict {
    
    Class cls = NSClassFromString(dict[@"class"]);
    NSAssert([cls isSubclassOfClass:[UIViewController class]], @"传入的控制器名错误");
    
    UIViewController *vc = [[cls alloc] init];
    
    vc.title = dict[@"title"];
    
    vc.tabBarItem.image = [[UIImage imageNamed:[NSString stringWithFormat:@"tabbar_%@", dict[@"image"]]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"tabbar_%@_selected", dict[@"image"]]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor orangeColor]} forState:UIControlStateSelected];
    
    return [[SNMainNaviController alloc] initWithRootViewController:vc];
}


@end




































